import StorageUtils from 'storageUtils';

const wxRequest = async (method = 'get', url, data, isFiler = true, callback) => {
  return new Promise((resolve, reject) => {
    wx.request({
      method: method,
      url: `${url}`,
      data: data,
      header: {
        'content-type': 'application/json;utf-8',
        'X-Access-Token': StorageUtils.get(StorageUtils.key.loginToken)
      },
      success: (data) => {
        if (isFiler) {
          // TODO 返回拦截
          if (data.statusCode !== 200) {
            // http请求失败
            reject();
          }
          if (data.data.code === 1) {
            // 请求成功
            resolve(data.data);
          } else if (data.data.code === 0) {
            // 系统错误
            wx.showToast({
              title: '服务器偷懒了~~',
              icon: 'none',
              duration: 2000
            });
            reject();
          } else {
            reject();
          }
        } else {
          resolve(data.data);
        }
      },
      fail: info => reject(info)
    });
  });
};

export default {
  wxRequest: wxRequest
};
